package com.ewili.x.special.service.data.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ewili.framework.utils.DateUtils;
import com.ewili.framework.utils.ExceptionUtils;
import com.ewili.framework.utils.StringUtils;
import com.ewili.x.special.dao.inter.SurveyStatDao;
import com.ewili.x.special.entity.SurveyStat;
import com.ewili.x.special.service.data.inter.SurveyStatService;
import org.apache.commons.lang3.ArrayUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;

/**
 * @author wonder
 */
@Service
public class SurveyStatServiceImpl implements SurveyStatService {

    @Autowired
    private SurveyStatDao surveyStatDao;

    @Override
    public void spide() {
        for (int i = 1; i < 10000; i++) {
            String url = "http://datainterface3.eastmoney.com/EM_DataCenter_V3/api/JGDYHZ/GetJGDYMX?js=datatable1205674&tkn=eastmoney&secuCode=&sortfield=0&sortdirec=1&pageNum=2&pageSize=50&cfg=jgdyhz&p=2&pageNo=2&_=1605669236121";
            boolean shouldSpide = this.doSpide(url);
            if (!shouldSpide) {
                break;
            }
        }
    }


    private boolean doSpide(String url) {
        Connection connection = Jsoup.connect(url).timeout(30000000);
        connection.maxBodySize(0);

        String content = null;
        try {
            content = connection.get().body().text();
        } catch (IOException e) {
            ExceptionUtils.propagate(e);
        }


        content = StringUtils.subString(content, "(").replace(")", "").replace("(", "");
        JSONObject object = JSON.parseObject(content);
        JSONArray jsonArray = object.getJSONArray("Data");
        JSONObject jObj = jsonArray.getJSONObject(0);
        Integer totalPage = jObj.getInteger("TotalPage");
        if (totalPage == 0) {
            return false;
        }

        JSONArray array = jObj.getJSONArray("Data");
        for (int j = 0; j < array.size(); j++) {
            String infoStr = array.get(j).toString();

            String[] strSplit = StringUtils.split(infoStr, "||");
            ArrayUtils.reverse(strSplit);
            System.out.println(ArrayUtils.toString(strSplit));

            int x = 4;
            if (strSplit.length == 11 && ArrayUtils.toString(strSplit).contains("公司")) {
                //x =
            }

            String persons = strSplit[2];
            String method = strSplit[3];
            String place = strSplit[x];
            Date publishDate = DateUtils.parse(strSplit[x + 1], DateUtils.yyyy_MM_dd);
            Date surveyDate = DateUtils.parse(strSplit[x + 2], DateUtils.yyyy_MM_dd);
            String chName = strSplit[x + 3];
            String code = strSplit[x + 4];
            Integer surveyCompanies = Integer.valueOf(strSplit[x + 5]);


            SurveyStat surveyStat = surveyStatDao.findByCodeAndSurveyDate(code, surveyDate);
            if (surveyStat == null) {
                surveyStat = new SurveyStat();
            }
/*

                surveyStat.setCode(code);
                surveyStat.setChName(chName);
                surveyStat.setMethod(method);
                surveyStat.setPersons(persons);
                surveyStat.setPlace(place);
                surveyStat.setSurveyCompanies(surveyCompanies);
                surveyStat.setSurveyDate(surveyDate);
                surveyStat.setPublishDate(publishDate);

                surveyStatDao.save(surveyStat);
*/
        }
        return false;
    }
}
